yjx.controller('productDetailController', ['$rootScope', '$scope', '$stateParams', '$state', '$fetch',  'modalService', '$timeout', 'wxShareService', '$init','htmlHandler','ipCookie', function ($rootScope, $scope, $stateParams, $state, $fetch,  modalService, $timeout, wxShareService, $init,htmlHandler,ipCookie) {
    $scope.nodata = false;
    var id = $stateParams.id;
    $init.title("产品详情")
    var HEIGHT = $('body').height();
    /*根据id获取产品详情*/
    function getDetail() {
        $scope.isProFetching = true;
        $fetch.fetch(
            '/product/detailById',
            {
                params: {
                    productId: id
                },
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.isProFetching = false;
                if(data.jbxx){
                    data.jbxx = htmlHandler.fontsizeHandler(data.jbxx);
                }
                $scope.productDetail = data;
                buildMainSwiper();
                // setAutoHeight();
                getComments();
                $scope.pdfAvatar = {
                    'background-image': 'url(' + data.pdfAvatar + ')'
                }
                $scope.pdfUrl = data.pdfUrl;
                var link = APPSRC+"#/productDetail?id="+$scope.productDetail.id
                wxShareService.onMenuShareTimeline('','','',link,'');
                wxShareService.onMenuShareAppMessage('','','',link,'');
            })
        }).catch(function (e) {
            $scope.isProFetching = false;
            modalService.mainMsgError(e.msg)
        })
    }
    if (id) {
        getDetail();
    }
    $scope.dldPdf = function (url) {
        $scope.url = url;
        $state.go('productDetail.pdfDetail')

    }
    $scope.miniheight={
        'min-height':HEIGHT - 250
    }
    /**swiper处理 */
    $scope.tabList = [{ id: 1, name: '基本信息' }, { id: 2, name: '适用人群' }, { id: 3, name: '服务流程' }, { id: 4, name: '免责条款' }, { id: 5, name: '报告模板' }, { id: 6, name: '评价' }];
    $scope.curTab = $scope.tabList[0];
    var curSwiper;
    var width = document.body.offsetWidth;
    var container = document.getElementById('proDetailSwiper');
    function buildMainSwiper() {
        $timeout(function () {
            var tabList = new Swiper('#tabList2', {
                slidesPerView: 3,
                slideToClickedSlide: true,
            });
            curSwiper = new Swiper('#proDetailSwiper', {
                direction: 'horizontal',
                //noSwiping : true,
                slider: width,
                on: {
                    slideNextTransitionStart: function () {
                        tabList.slideTo(curSwiper.activeIndex);
                        $timeout(function () {
                            $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                        })
                    },
                    slidePrevTransitionStart: function () {
                        tabList.slideTo(curSwiper.activeIndex - 1);
                        $timeout(function () {
                            $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                        })
                    },
                    slideChangeTransitionEnd: function () {
                        setAutoHeight();
                    }

                }
            })
            // setAutoHeight(); 
        }, 1000)
    }


    $scope.tabSelect = function (item, index) {
        $scope.curTab = item;
        curSwiper.slideTo(index, 500);
    }
    /**swiper自适应内容高度 */
    function setAutoHeight() {
        // $timeout(function () {
            // angular.element(container).height(height);
            var height = angular.element(container.querySelector('.swiper-slide-active')).find('>div').height();
            
            // if(height<200){
            //     height=200;
            // }
            angular.element(container).height(height);
            // alert( angular.element(container).height());
        // },500)
    }
    /**评论相关 */
    var commentApi = '/product/comments';
    var params = {
        offset: 1,
        limit: 10
    }
    $scope.pageNum = 1;
    $scope.hideShows = true;
    function getComments() {
        params.productId = $scope.productDetail.id;
        $fetch.fetch(
            commentApi,
            {
                params: params
            }
        ).then(function (data) {
            $timeout(function () {
                if (data) {
                    $scope.dataList = data.list;
                    $scope.paging = data.page;
                    //更新状态、数据加载控制
                    $scope._updated = 'reupdated';
                    $scope.hideShows = false;
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**评论翻页 */
    $scope.list = function () {
        params.offset = $scope.pageNum;
        params.productId = $scope.productDetail.id;
        $fetch.fetch(
            commentApi,
            {
                params: params,
                cache: false,
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.paging = data.page;
                //$scope.dataList = data.rows;
                //if($scope.dataList)
                $scope.dataList = $scope.dataList.concat(data.list);
                //更新状态、数据加载控制
                $scope._updated = 'updated';
                $scope.hideShows = false;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }

    $scope.temple = '<li ng-repeat="item in dataList" ><comment-item></comment-item></li>'
    /**客服 */
    $scope.chat = function(){
        // ipCookie.remove('productList',ipCookiesParam);
       
        var product = {
            id:$scope.productDetail.id,
            name:$scope.productDetail.name,
            describe:$scope.productDetail.describe,
            price:$scope.productDetail.price,
            originalPrice:$scope.productDetail.originalPrice,
            supplierAvatar:$scope.productDetail.supplierAvatar,
        }
        var productList = [product];
        $timeout(function(){
            ipCookie('productList',productList,ipCookiesParam);
            $state.go('chat');
        })
      
    }
    /**下单预约等更多操作 */
    $scope.showMoreActs = function () {
        if ($rootScope.identity == 1) {
            modalService.actsModals();
        } else {
            goOrder();
        }

    }
    $scope.itemList = [
        { id: 1, name: '下单', callback: goOrder },
        { id: 2, name: '预约', callback: goReservation },
    ]
    function goOrder() {
        close();
        $state.go('shopcart.orderPay',{forwardId:$scope.forwardId,productIds:$scope.productDetail.id})
    }
    function goReservation() {
        close();
        $state.go('reservation',{id:$scope.productDetail.id})
    }
    // function forwardOrder() {
    //     close();
    //     /*获取微信转发配置*/
    //     var wxDesc = $scope.productDetail.title;
    //     /*android分享链接添加#时自带参数会被微信自动添加的form等参数截掉  原因未知*/
    //     if ($rootScope.identity == 1) {
    //         if ($scope.isAndroid) {
    //             $scope.link = APPSRC + "?url=productDetail&id=" + $scope.productDetail.id + "&forwardId=" + $rootScope.userId;
    //         } else {
    //             $scope.link = APPSRC+'#/' + "productDetail?id=" + $scope.productDetail.id + "&forwardId=" + $rootScope.userId;           
    //         }       
    //     }else{
    //         if ($scope.isAndroid) {
    //             $scope.link = APPSRC + "?url=productDetail&id=" + $scope.productDetail.id
    //         } else {
    //             $scope.link = APPSRC+'#/' + "productDetail?id=" + $scope.productDetail.id;           
    //         } 
    //     }
    //     wxShareService.onHideMenuItems();
    //     wxShareService.onMenuShareAppMessage('',wxDesc,'',$scope.link,$scope.callback)
        
    //     modalService.mainMsgInfo("点击右上角将产品发送给客户下单")
    // }

    /**加入购物车 */
    $scope.addShopcart = function(){
        $fetch.fetch(
            '/shopping/goods/add',
            {
                params:{
                    productId:$scope.productDetail.id
                },
                type:'post'
            }
        ).then(function(data){
            modalService.mainMsgOk("该产品已加入购物车");
        }).catch(function(e){
            if(e.code == 2002){
                modalService.mainMsgOk("该产品已加入购物车");
            }else{
                modalService.mainMsgError(e.msg);
            }
        })
    }

    $scope.close = function () {
        close();
    }
    function close() {
        $rootScope.modalStatus = 'modal-hide';
        $rootScope.modalTag = "actsModals";
    }

}])